/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-17<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.service.library.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.cnsy123.bookwebsiteserver.entity.SysBookLibraryInfo;
import cn.cnsy123.bookwebsiteserver.repository.SysLibraryInfoDao;
import cn.cnsy123.bookwebsiteserver.service.library.SysLibraryInfoService;
import cn.cnsy123.bookwebsiteserver.vo.layui.CommonResult;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-17<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@Service
public class SysLibraryInfoServiceImpl implements SysLibraryInfoService {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SysLibraryInfoDao sysLibraryInfoDao;

    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = true)
    public CommonResult<Object> findLibraryManagerList() {
        CommonResult<Object> result = new CommonResult<Object>();
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT user.user_realname as realname,user.user_id as userId  from sys_user_detail_info user left join sys_role_info role on role.role_id = user.USER_ROLE_ID where role.role_name = '馆长'");
            List<Map<String, String>> list = jdbcTemplate.query(sql.toString(), new RowMapper<Map<String, String>>() {
                @Override
                public Map<String, String> mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Map<String, String> map = new HashMap<String,String>();
                    map.put("name", rs.getString(1));
                    map.put("value", rs.getString(2));
                    return map;
                }
                
            });
            if(!CollectionUtils.isEmpty(list)) {
                result.setCode(0);
                result.setMsg("获取馆长角色人员列表成功");
                result.setData(list);
            }else {
                result.setCode(1);
                result.setMsg("暂无获取馆长角色人员列表");
            }
        } catch (Exception e) {
            throw new RuntimeException("获取馆长角色人员列表失败,原因:"+e.getMessage());
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = true)
    public CommonResult<Object> findLibraryInfo() {
        CommonResult<Object> result = new CommonResult<Object>();
        try {
            List<SysBookLibraryInfo> list = sysLibraryInfoDao.findAll();
            if(!CollectionUtils.isEmpty(list)) {
                result.setCode(0);
                result.setMsg("获取馆长角色人员列表成功");
                result.setData(list.get(0));
            }else {
                result.setCode(1);
                result.setMsg("暂无获取馆长角色人员列表");
            }
        } catch (Exception e) {
            throw new RuntimeException("获取馆长角色人员列表失败,原因:"+e.getMessage());
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public CommonResult<Object> saveLibraryInfo(SysBookLibraryInfo sysBookLibraryInfo) {
        CommonResult<Object> result = new CommonResult<Object>();
        try {
            sysLibraryInfoDao.save(sysBookLibraryInfo);
            result.setCode(0);
            result.setMsg("保存图书馆信息成功");
        } catch (Exception e) {
            throw new RuntimeException("保存图书馆信息失败,原因:"+e.getMessage());
        }
        return result;
    }

}
